package com.usian.wemedia.controller;

import com.usian.api.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmUserService wmUserService;

    @Override
    @PostMapping("/list")
    public ResponseResult findPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findPage(dto);
    }

    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        if(dto.getStatus()== WmNews.Status.SUBMIT.getCode()){
            //提交文章
            return wmNewsService.saveNews(dto, WmNews.Status.SUBMIT.getCode());
        }else{
            //保存草稿
            return wmNewsService.saveNews(dto, WmNews.Status.NORMAL.getCode());
        }
    }

    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findByNewId(@PathVariable Integer id) {
        return wmNewsService.findByNewId(id);
    }


    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult deleteByNewId(@PathVariable Integer id) {
        return wmNewsService.deleteByNewId(id);
    }

    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    @Override
    @GetMapping("/ones/{id}")
    public WmNews findByNewsId(@PathVariable Integer id) {
        WmNews wmNews = wmNewsService.getById(id);
        return wmNews;
    }

    @Override
    @PostMapping("/updateNewStatus")
    public ResponseResult updateNewStatus(@RequestBody WmNews wmNews){
        return wmNewsService.updateNewStatus(wmNews);
    }

    @Override
    @GetMapping("/findByUserId/{id}")
    public WmUser findByUserId(@PathVariable("id") Integer userId){
        return wmUserService.findByUserId(userId);
    }

    @Override
    @PostMapping("/findNews")
    public List<Integer> findNews() {
        return wmNewsService.findNews();
    }

    @Override
    @PostMapping("/findList")
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }

    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }


}
